/* <one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2011  Joseph Luccisano

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.lucciframework.validator.string
{
	/**
	 * This class allows checking a string.
	 * Different parameters allow to validate a string the number of 
	 * characters. 
	 */
	
	//Ajouter la gestion des Regular Expression
	
	import mx.containers.VBox;
	import mx.core.IMXMLObject;
	import mx.validators.StringValidator;
	
	import org.lucciframework.validator.core.AbstractValidator;
	
	public class LucciStringValidator extends AbstractValidator implements IMXMLObject
	{		
		private var _stringValidator:StringValidator;
			
		private var _minLength:String;
		

		[Inspectable(defaultValue="0")]
		/**
		 * This attribute allows to set number min of characters that the string can contain
		 * 
		 * default value is 0
		 */
		public function get minLength () : String {
			return this._minLength;
		}
		
		/**
		* @private
		*/
		public function set minLength ( value : String ) : void {			
			_minLength = value;
		}

		
		private var _maxLength:String;
		
		[Inspectable(defaultValue="100")]
		/**
		 * This attribute allows to set number max of characters that the string can contain
		 * 
		 * default value is 100
		 */
		public function get maxLength () : String {
			return this._maxLength;
		}
	
		/**
		 * @private
		 */
		public function set maxLength ( value : String ) : void {
			_maxLength = value;	
		}
		
		private var _requiredFieldError:String;
		
		/**
		 * 
		 */
		public function get requiredFieldError():String
		{
			return _requiredFieldError;
		}
		
		/**
		 * 
		 */
		public function set requiredFieldError(value:String):void
		{
			_requiredFieldError = value;
		}
		
		
		private var _tooLongError:String;

		/**
		 * 
		 */
		public function get tooLongError():String
		{
			return _tooLongError;
		}

		/**
		 * 
		 */
		public function set tooLongError(value:String):void
		{
			_tooLongError = value;
		}
		
			
		private var _tooShortError:String;

		/**
		 * 
		 */
		public function get tooShortError():String
		{
			return _tooShortError;
		}

		/**
		 * 
		 */
		public function set tooShortError(value:String):void
		{
			_tooShortError = value;
		}
		
				
		
		/**
		 * @private
		 */
		override public function validate () : void {
			
			validationResultEvent = _stringValidator.validate( dataToValidate );
			
			validationResultEventArray = [ validationResultEvent ];
		}

		/**
		 * @private
		 */
	    public function initialized ( document : Object , id : String ) : void {
			
			_stringValidator = new StringValidator();
			_stringValidator.minLength = _minLength;
			_stringValidator.maxLength = _maxLength;
			_stringValidator.requiredFieldError = _requiredFieldError;
			_stringValidator.tooLongError = _tooLongError;
			_stringValidator.tooShortError = _tooShortError;
			_stringValidator.listener = this.target;
			_stringValidator.required = this.required;
		}
	}
}